package chapter5.lessons.udpDemo02;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * @Project ：learn_java
 * @File ：SendMessage.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/12 20:39
 * @Description ：For study.
 */

public class SendMessage {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();

        // 打包数据
        Scanner scanner = new Scanner(System.in);

        // ctrl + alt + t 选择代码的包围方式
        while (true) {
            System.out.print("请输入你想要发送的话：");
            String str = scanner.nextLine();
            if ("886".equals(str)) {
                break;
            }

            byte[] bytes = str.getBytes();

            InetAddress address = InetAddress.getByName("127.0.0.1");

            int port = 10086;

            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);

            // 发送数据
            ds.send(dp);
        }

        // 释放资源
        ds.close();
    }
}
